<?php
/**
* @link http://framework.org/
* @copyright Copyright: 2003-2004 Doutromundo
* @author Gregory Brown <bugzbrown@gmail.com>
* @access public
* @package core
*/
/**
* BREADCRUMB (PHP5)
*
* Leave a breadcrumb trail for linear navigation.
* IMPORTANT - Sessions must be enabled for this class to work
*
* @todo Must make it more intelligent - allow it to work without printing
* @version 0.4.0
* @package core
*/
class crumb {
	/**
	 * @return void
	 * @param level int
	 * @param title string
	 * @param url string
	 * @param post boolean
	 * @desc Add a bread crumb to the session array. If post is true add the $_POST args to the URL.
	 */
	function addCrumb($level, $title, $url, $post = false) {

		$tstr = "";

		if (isset($_SESSION['crumbs'][$level])){
			unset($_SESSION['crumbs'][$level]);
		}

		if($post){
			if(strpos($url,"?")){
				$tstr = "&";
			}else{
				$tstr = "?";
			}

			foreach($_POST as $key => $value) {
				$tstr.=$key."=".urlencode($value)."&";
			}
			// pop off the last &
			$tstr = rtrim ($tstr, "&");
		}


		$tmp = array("title" => $title, "url" => $url . $tstr);
		$_SESSION['crumbs'][$level] = $tmp;
	} //end addCrumb()


	/**
	 * @return void
	 * @param level int
	 * @desc Deletes a bread crumb.
	 */
	function delCrumb($level) {
		if (isset($_SESSION['crumbs'][$level])){
			unset($_SESSION['crumbs'][$level]);
		}
	} //end delCrumb()


	/**
	 * @return void
	 * @param int cur_level
	 * @param string separator : The string uses as a separator
	 * @param string linkClass : Name for the class responsible for showing links on crumb
	 * @param string noLinkClass : Name for the class responsible for showing items without links
	 * @desc Print out the current crumb trail from $cur_level on down.
	 */
	function printTrail($cur_level,$separator="&nbsp;&gt;&nbsp;",$linkClass="crumb_href",$noLinkClass="crumb_span") {

		echo "<span class=\"$noLinkClass\">";
		for ($i=1; $i != $cur_level+1; $i++){

			if (isset($_SESSION['crumbs'][$i])){
				if ($i != $cur_level){
						echo "<a class=\"$linkClass\" href=\"". $_SESSION['crumbs'][$i]['url'] . "\">";
						echo $_SESSION['crumbs'][$i]['title'];
						echo "</a>";
				}else{
					echo $_SESSION['crumbs'][$i]['title'];
					echo "</span>";
				}
				if ($i != $cur_level){
					echo $separator;
				}
			}
		}
		echo "</span>";
	} // end printTrail()
} //end class crumb
?>
